﻿(function () {
    eS = (function () {
        eS = function () {
            return new eS.fn.Create();
        }
        eS.fn = eS.prototype = {
            Selector: "",
            fn: eS.fn,
            Mask: {
                cnpj: '99.999.999/9999-99',
                money: '99,999999',
                cpf: '',
                telefone: '',
                celular: '',
                calendar: '',
                number: '',
                cep: '',
                tudo: '*',
                charAndNum: '*',
                lowerChar: 'a',
                upperChar: 'Z',
                decimal: ''
            },
            Create: function () {
                var $obj = eS.fn;
                //privateFn();
                return $obj;
            }
        }
        // fn privada
        var privateFn = function () {
            alert('');
        }

        var __trigger = function (element, name, args, returnValue) {
            // cria os parametros
            var __param = ""
            for (var i in args) {
                __param += (__param.length > 0 ? ", " : "") + "e." + i;
            }
            // cria a função anônima para disparo
            var ___name = element.getAttribute(name.indexOf("on") > -1 ? name : ("on" + name)) || "";
            if (___name.indexOf("(") > -1) {
                ___name = ___name.substr(0, ___name.indexOf("("))
            }
            var __func = Function("e",
                    "with(document){" +
                        "with(this){" +
                            (returnValue ? "return " : "") + ___name +
                            "(" + __param + ")" +
                        "}" +
                    "}"
                 );
            // invoca o evento
            return __func.call(element, args);
        }

        eS.forEachObjectMask = function (jQ, maskName, objectMask) {
            if (objectMask == undefined)
                return;
            obj = {
                name: '',
                mascara: ''
            }
            for (var i in objectMask) {
                if (i != maskName)
                    continue;
                obj.name = i;
                obj.mascara = objectMask[i];
                if (obj.mascara == objectMask.decimal || obj.mascara == objectMask.money)
                    eS.setMask(jQ, obj.mascara, "reverse");
                else
                    eS.setMask(jQ, obj.mascara);
                break;
            }
        }
        eS.GetMask = function (jQ, mask) {
            if (mask == undefined || mask == null)
                return;
            return mask[$(jQ).attr('mask')];
        }
        eS.GetRange = function (jQ) {
            return parseInt($(jQ).attr('range'));
        }
        eS.setMask = function (jQ, mask, typeItem) {
            if (typeItem)
                $(jQ).setMask({ mask: mask, autoTab: false, type: typeItem });
            else
                $(jQ).setMask({ mask: mask, autoTab: false });
        }
        eS.UrlPadrao = '';
        // fn static
        eS.staticFn = function () {
            alert(this.UrlPadrao);
        }
        eS.ConfigureMasks = function (mask) {
            if (mask == undefined)
                return;
            $("input[range]").each(function () {
                if (mask == undefined || mask == null)
                    return;
                var rang = eS.GetRange(this);
                if (rang == NaN)
                    return;
                //var _mask = mask.number;
                var _mask = eS.GetMask(this, mask);
                var newMask = '';
                for (i = 0; i < rang; i++) {
                    newMask += _mask;
                }
                eS.setMask(this, newMask);
            });
            $("input[mask]").each(function () {
                range = $(this).attr("range")
                if (range != undefined && range != null)
                    return;
                maskName = $(this).attr('mask');
                eS.forEachObjectMask(this, maskName, mask);
            });
            $("input[custommask]").each(function () {
                maskName = $(this).attr('custommask');
                eS.setMask(this, maskName, $(this).attr("reverse") || "");
            });
        }

        eS.SetCalendars = function () {
            function CreateCalendar(jQ, config) {
                date = $(jQ).attr("max-year") || "0";
                var newAno = (new Date().getFullYear() + parseInt(date, 10));
                ano = (newAno).toString();
                var min = $(jQ).attr("min-date") || "1970";
                var minValue = min == "now" ? new Date() : new Date(parseInt(min, 10), 1 - 1, 1);
                if (min == "now") {
                    min = (new Date().getFullYear()).toString();
                }
                value = $(jQ).attr("onDataSelect");
                selected = !value || value == null ? function () {

                } :
                function (j, e) {
                    var day = e.selectedDay > 9 ? e.selectedDay.toString() : "0" + e.selectedDay.toString();
                    var month = e.selectedMonth + 1 > 9 ? (e.selectedMonth + 1).toString() : "0" + (e.selectedMonth + 1).toString();
                    var year = e.selectedYear > 9 ? e.selectedYear.toString() : "0" + e.selectedYear.toString();
                    return __trigger(e.input.get(0), "onDataSelect", { date:
                                day + "/" +
                                month + "/" +
                                year
                    }, true);
                };

                config.minDate = minValue;
                if (newAno > new Date().getFullYear())
                    config.yearRange = min + ':' + ano;
                else
                    delete config.yearRange;
                config.onSelect = selected;
                $(jQ).datepicker(config)

            }

            $("input[date]").each(function (e) {
                date = $(this).attr("max-year") || "0";
                ano = (new Date().getFullYear() + parseInt(date, 10)).toString();
                var min = $(this).attr("min-date") || "1970";
                var minValue = min == "now" ? new Date() : new Date(parseInt(min, 10), 1 - 1, 1);
                if (min == "now") {
                    min = (new Date().getFullYear()).toString();
                }
                selected = $(this).attr("onDataSelected");
                selected = selected || selected == null ? function () { } : function () {

                };
                $(this).datepicker({
                    showOn: "button",
                    buttonImage: eS.UrlPadrao + "Recursos/i/calendar.png",
                    buttonImageOnly: true,
                    altFormat: "dd/MM/yy",
                    changeYear: true,
                    yearRange: min + ':' + ano,
                    minDate: minValue,
                    onSelect: selected
                });
            });
            $("div[calendar='true']").each(function (e) {
                CreateCalendar(this, {
                    buttonImageOnly: true,
                    changeMonth: true,
                    altFormat: "dd/MM/yy",
                    changeYear: true,
                    yearRange: '',
                    minDate: '',
                    onSelect: new Function()
                });
            });
        }
        //window.eS = eS;
        return eS(); //.fn;
    });
    window.eS = eS;
    return eS();
})(window);